class ListNode {
  val: number
  next: ListNode | null

  constructor(val?: number, next?: ListNode | null) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
  }
}

function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
  if (headA === null || headB === null) {
    return null
  }
  let pa = headA
  let pb = headB
  while (pa != pb) {
    pa = pa == null ? headB : pa.next!
    pb = pb == null ? headA : pb.next!
  }
  return pa
};
